package com.yanqu.road.logic.activity.hotpotjump.game;

import com.yanqu.road.entity.autodata.hotpotjump.HotpotJumpGrid;
import com.yanqu.road.entity.autodata.hotpotjump.HotpotJumpUnit;

import java.util.*;

public class HotpotJumpBoard {
    private final static int size = 11; // 棋盘的大小
    public JumpBoardGrid[][] board;
    private Set<JumpBoardGrid> outLineGrids;//轮廓点

    // 构造函数初始化棋盘
    public HotpotJumpBoard() {
        this.board = new JumpBoardGrid[size][size];
        initializeBoard();
    }

    // 初始化棋盘
    private void initializeBoard() {
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                board[y][x] = new JumpBoardGrid(x, y);
            }
        }

        //指定第一个点的q和r为0
        JumpBoardGrid firstGrid = board[0][0];
        firstGrid.setQ(0);
        firstGrid.setR(0);
        List<JumpBoardGrid> pathGrids = new ArrayList<>();
        pathGrids.addAll(findPathGrids(firstGrid, eJumpDirection.RightDown, -1, true));
        for (int r = 0; r < pathGrids.size(); r++) {
            JumpBoardGrid grid = pathGrids.get(r);
            grid.setR(r);
            grid.setQ(0);

            List<JumpBoardGrid> leftGrids = findPathGrids(grid, eJumpDirection.Left, -1, false);
            for (int i1 = 0; i1 < leftGrids.size(); i1++) {
                JumpBoardGrid leftGrid = leftGrids.get(i1);
                leftGrid.setR(r);
                leftGrid.setQ(0 - 1 - i1);//左边的点递减
            }

            List<JumpBoardGrid> rightGrids = findPathGrids(grid, eJumpDirection.Right, -1, false);
            for (int i1 = 0; i1 < rightGrids.size(); i1++) {
                JumpBoardGrid rightGrid = rightGrids.get(i1);
                rightGrid.setR(r);
                rightGrid.setQ(1 + i1);//右边的点递增
            }
        }

        //第一行的第1个和最后两个元素清掉
        board[0][0] = null;
        board[0][size - 1] = null;
        board[0][size - 2] = null;
        //第二行的第一个和最后一个元素清掉
        board[1][0] = null;
        board[1][size - 1] = null;
        //第三行最后一个元素清掉
        board[2][size - 1] = null;
        //倒数第一行的第1个和最后两个元素清掉
        board[size - 1][0] = null;
        board[size - 1][size - 1] = null;
        board[size - 1][size - 2] = null;
        //倒数第二行的第一个和最后一个元素清掉
        board[size - 2][0] = null;
        board[size - 2][size - 1] = null;
        //倒数第三行最后一个元素清掉
        board[size - 3][size - 1] = null;

        //第五行最后一个和倒数第五行最后一个元素清掉
        board[4][size - 1] = null;
        board[size - 5][size - 1] = null;

        //得到轮廓点
        outLineGrids = new HashSet<>();
        for (JumpBoardGrid[] grids : board) {
            for (JumpBoardGrid grid : grids) {
                if (grid == null) {
                    continue;
                }
                List<JumpBoardGrid> neighbors = getNeighbors(grid);
                if (neighbors.size() != 6) {//没有被围起来的点都是轮廓点
                    outLineGrids.add(grid);
                }
            }
        }
    }

    // 创建重复字符串的辅助方法
    public String repeat(String str, int count) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < count; i++) {
            builder.append(str);
        }
        return builder.toString();
    }

    /**
     * 越界判断
     */
    public boolean isOutOfBounds(int x, int y) {
        return x < 0 || x >= size || y < 0 || y >= size;
    }

    public JumpBoardGrid findGrid(int x, int y) {
        if (isOutOfBounds(x, y)) {
            return null;
        }
        return board[y][x];
    }

    /**
     * 这个方法会生成临时的坐标格子,只在需要取坐标集合里用
     *
     * @return
     */
    public JumpBoardGrid findGridNewIfNull(int x, int y) {
        if (isOutOfBounds(x, y)) {
            return new JumpBoardGrid(x, y);
        }
        return board[y][x];
    }

    /**
     * 六个方向周围所有的点,N个圈的点
     */
    public List<JumpBoardGrid> findSurroundGrids(JumpBoardGrid center, int layers) {
//        Set<JumpBoardGrid> grids = new HashSet<>();
//        grids = findSurroundingGridsIterative(center, layers);
//        grids.remove(center);//中心点去掉
        List<JumpBoardGrid> grids = new ArrayList<>();
        for (int distance = 1; distance <= layers; distance++) {
            grids.addAll(findClockwiseGrids(center, distance));
        }
        return grids;
    }

    public Set<JumpBoardGrid> findSurroundingGridsIterative(JumpBoardGrid center, int layers) {
        Set<JumpBoardGrid> points = new HashSet<>();
        Queue<JumpBoardGrid> queue = new LinkedList<>();

        // 初始化队列
        queue.offer(center);
        points.add(center);

        // 逐层遍历
        for (int layer = 0; layer < layers; layer++) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                JumpBoardGrid current = queue.poll();
                List<JumpBoardGrid> neighbors = getNeighbors(current);
                for (JumpBoardGrid neighbor : neighbors) {
                    if (!points.contains(neighbor)) {
                        queue.offer(neighbor);
                        points.add(neighbor);
                    }
                }
            }
        }

        return points;
    }

    public JumpBoardGrid findLeftGrid(int x, int y, boolean newIfNull) {
        int findY = y;
        int findX = x - 1;
        if (newIfNull) {
            return findGridNewIfNull(findX, findY);
        } else {
            return findGrid(findX, findY);
        }
    }

    public JumpBoardGrid findRightGrid(int x, int y, boolean newIfNull) {
        int findY = y;
        int findX = x + 1;
        if (newIfNull) {
            return findGridNewIfNull(findX, findY);
        } else {
            return findGrid(findX, findY);
        }
    }

    public JumpBoardGrid findLeftUpGrid(int x, int y, boolean newIfNull) {
        int findY = -1;
        int findX = -1;
        if (y % 2 == 0) {
            findY = y - 1;
            findX = x;
        } else {
            findY = y - 1;
            findX = x - 1;
        }
        if (newIfNull) {
            return findGridNewIfNull(findX, findY);
        } else {
            return findGrid(findX, findY);
        }
    }

    public JumpBoardGrid findRightUpGrid(int x, int y, boolean newIfNull) {
        int findY = -1;
        int findX = -1;
        if (y % 2 == 0) {
            findY = y - 1;
            findX = x + 1;
        } else {
            findY = y - 1;
            findX = x;
        }
        if (newIfNull) {
            return findGridNewIfNull(findX, findY);
        } else {
            return findGrid(findX, findY);
        }
    }

    public JumpBoardGrid findLeftDownGrid(int x, int y, boolean newIfNull) {
        int findY = -1;
        int findX = -1;
        if (y % 2 == 0) {
            findY = y + 1;
            findX = x;
        } else {
            findY = y + 1;
            findX = x - 1;
        }
        if (!isOutOfBounds(findX, findY)) {
            JumpBoardGrid jumpBoardGrid = board[findY][findX];
            return jumpBoardGrid;
        }
        return null;
    }

    public JumpBoardGrid findRightDownGrid(int x, int y, boolean newIfNull) {
        int findY = -1;
        int findX = -1;
        if (y % 2 == 0) {
            findY = y + 1;
            findX = x + 1;
        } else {
            findY = y + 1;
            findX = x;
        }
        if (newIfNull) {
            return findGridNewIfNull(findX, findY);
        } else {
            return findGrid(findX, findY);
        }
    }


    /**
     * 周围一圈的点
     *
     * @return
     */
    public List<JumpBoardGrid> getNeighbors(JumpBoardGrid centerGrid) {
        List<JumpBoardGrid>  grids = new ArrayList<>();
        for (eJumpDirection direction : eJumpDirection.getAllValidDirections()) {
            List<Integer> pos = centerGrid.findDirectionNeighborPos(direction);
            int x = pos.get(0);
            int y = pos.get(1);
            JumpBoardGrid grid = findGrid(x, y);
            if (grid != null) {
                grids.add(grid);
            }
        }
        return grids;
    }

    /**
     * 在指定格子列表中找指定格子的周围格子
     *
     * @param center
     * @param grids
     * @return
     */
    public static Set<JumpBoardGrid> findNeighborGridsInList(JumpBoardGrid center, Set<JumpBoardGrid> grids) {
        Set<JumpBoardGrid> neighborGrids = new HashSet<>();
        for (JumpBoardGrid grid : grids) {
            if (center.isNeighbor(grid)) {
                neighborGrids.add(grid);
            }
        }
        return neighborGrids;
    }

    /**
     * 找指定格子中能组成三角的
     *
     * @param grids
     * @return
     */
    public Set<JumpBoardGrid> findTriangles(Set<JumpBoardGrid> grids) {
        Set<Set<JumpBoardGrid>> triangles = new HashSet<>();//n个三角形

        Set<JumpBoardGrid> results = new HashSet<>();

        for (JumpBoardGrid grid : grids) {
            Set<JumpBoardGrid> neighbors = findNeighborGridsInList(grid, grids);
            for (JumpBoardGrid neighbor1 : neighbors) {
                if (grids.contains(neighbor1)) {
                    for (JumpBoardGrid neighbor2 : neighbors) {
                        if (!neighbor1.equals(neighbor2) && grids.contains(neighbor2) && neighbor1.isNeighbor(neighbor2)) {
                            Set<JumpBoardGrid> triangle = new HashSet<>();
                            triangle.add(grid);
                            triangle.add(neighbor1);
                            triangle.add(neighbor2);
                            triangles.add(triangle);

                            results.add(grid);
                        }
                    }
                }
            }
        }
        return results;
    }

    /**
     * 找到两个点之间的所有点
     *
     * @return
     */
    public List<JumpBoardGrid> findPathGridsBetweenTowGrids(JumpBoardGrid start, JumpBoardGrid end) {
        eJumpDirection direction = start.calcTargetDirection(end);
        int distance = start.distanceTo(end);
        return findPathGrids(start, direction, distance, true);
    }

    /**
     * 找到指定点指定方向的所有点,不包含指定点
     *
     * @param start        起始点
     * @param direction    方向
     * @param distance     选取距离
     * @param includeStart 是否包含起始点
     */
    public List<JumpBoardGrid> findPathGrids(JumpBoardGrid start, eJumpDirection direction, int distance, boolean includeStart) {
        List<JumpBoardGrid> grids = new ArrayList<>();
        if (distance == -1) {
            distance = size * 2;//最大移动距离
        }
        if (includeStart) {
            grids.add(start);//包含起始点
        }
        JumpBoardGrid findGrid = start;
        for (int i = 0; i < distance; i++) {
            List<Integer> pos = findGrid.findDirectionNeighborPos(direction);
            int x = pos.get(0);
            int y = pos.get(1);
            findGrid = findGrid(x, y);
            if (findGrid != null) {
                grids.add(findGrid);
            } else {
                break;
            }
        }

        return grids;
    }

    /**
     * 两个点是否直线
     *
     * @return
     */
    public boolean arePointsCollinear(JumpBoardGrid grid1, JumpBoardGrid grid2) {
        return arePointsCollinear(grid1.getQ(), grid1.getR(), grid2.getQ(), grid2.getR());
    }

    /**
     * 根据QR轴判断点是否直线
     *
     * @param q1
     * @param r1
     * @param q2
     * @param r2
     * @return
     */
    public boolean arePointsCollinear(int q1, int r1, int q2, int r2) {
        if (r1 == r2) {
            return true;
        }

        // 检查是否在左下到右上的对角线上
        if ((q1 + r1) == (q2 + r2)) {
            return true;
        }

        // 检查是否在左上到右下的对角线上
        if (q1 == q2) {
            return true;
        }

        return false;
    }

    /**
     * 找到点集合包围起来的点,不包含边界
     */
    public Set<JumpBoardGrid> findEndCloseGrids(Set<JumpBoardGrid> boundaryGrids) {
        Stack<JumpBoardGrid> stack = new Stack<>();
        //轮廓点中不是边界的点
        Set<JumpBoardGrid> stackGrids = new HashSet<>(outLineGrids);
        stackGrids.removeAll(boundaryGrids);
        stack.addAll(stackGrids);

        Set<JumpBoardGrid> filledPoints = new HashSet<>();//填充没被包含的点

        while (!stack.isEmpty()) {
            JumpBoardGrid point = stack.pop();
            if (filledPoints.contains(point) || boundaryGrids.contains(point)) {
                continue;
            }

            // Fill point
            filledPoints.add(point);

            // Push neighbors
            for (JumpBoardGrid neighbor : getNeighbors(point)) {
                if (!filledPoints.contains(neighbor) && !boundaryGrids.contains(neighbor) && !outLineGrids.contains(neighbor)) {
                    stack.push(neighbor);
                }
            }
        }

        Set<JumpBoardGrid> enclosedGrids = new HashSet<>();//被包含的点
        Set<JumpBoardGrid> remainBoundaryGrids = new HashSet<>(boundaryGrids);//边界点从这里慢慢排除
        for (int x = -size; x <= size; x++) {
            for (int y = -size; y <= size; y++) {
                JumpBoardGrid grid = findGrid(x, y);
                if (grid == null) {
                    continue;
                }
                //排除盘面边界的点
                if (outLineGrids.contains(grid)) {
                    continue;
                }
                if (boundaryGrids.contains(grid)) {//划线的点不算
                    continue;
                }
                if (filledPoints.contains(grid)) {//被填充的点不算
                    continue;
                }

                enclosedGrids.add(grid);
            }
        }

        return enclosedGrids;
    }

    /**
     * 找到有交叉的点的交叉方向上所有的点
     *
     * @return
     */
    public Set<JumpBoardGrid> findCrossGridsAndPath(Map<JumpBoardGrid, Set<eJumpDirection>> gridDirectionMap, int distance) {
        Set<JumpBoardGrid> result = new HashSet<>();

        for (Map.Entry<JumpBoardGrid, Set<eJumpDirection>> entry : gridDirectionMap.entrySet()) {
            JumpBoardGrid grid = entry.getKey();
            Set<eJumpDirection> directions = entry.getValue();
            if (directions.size() > 2) {//是交叉点,拿交叉的所有方向的点
                result.add(grid);
                result.addAll(findCrossPathGrids(grid, directions, distance));
                for (eJumpDirection direction : directions) {
                    List<JumpBoardGrid> pathGrids = findPathGrids(grid, direction, distance, false);
                    result.addAll(pathGrids);

                    eJumpDirection oppositeDirection = direction.getOppositeDirection();
                    if (!directions.contains(oppositeDirection)) {
                        List<JumpBoardGrid> pathGrids2 = findPathGrids(grid, oppositeDirection, distance, false);
                        result.addAll(pathGrids2);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 获得交叉路径的点
     *
     * @return
     */
    public Set<JumpBoardGrid> findCrossPathGrids(JumpBoardGrid centerGrid, Set<eJumpDirection> directions, int distance) {
        if (directions == null || directions.isEmpty()) {
            return new HashSet<>();
        }
        Set<JumpBoardGrid> result = new HashSet<>();
        for (eJumpDirection direction : directions) {
            List<JumpBoardGrid> pathGrids = findPathGrids(centerGrid, direction, distance, false);
            result.addAll(pathGrids);

            eJumpDirection oppositeDirection = direction.getOppositeDirection();
            if (!directions.contains(oppositeDirection)) {
                List<JumpBoardGrid> pathGrids2 = findPathGrids(centerGrid, oppositeDirection, distance, false);
                result.addAll(pathGrids2);
            }
        }
        return result;
    }

    /**
     * 找到起点到终点的最小移动路径
     */
    public List<eJumpDirection> calcMinMovePath(JumpBoardGrid startGrid, JumpBoardGrid endGrid, int moveNum) {
        List<eJumpDirection> result = new ArrayList<>();
        int distance = startGrid.distanceTo(endGrid);
        if (distance <= 1) {
            return result;
        }
        JumpBoardGrid iterGrid = startGrid;
        for (int i = 1; i <= moveNum; i++) {
            List<JumpBoardGrid> surroundGrids = findSurroundGrids(iterGrid, 1);
            for (JumpBoardGrid surroundGrid : surroundGrids) {
                int surroundDistance = surroundGrid.distanceTo(endGrid);
                if (surroundDistance < distance){
                    distance = surroundDistance;
                    eJumpDirection direction = iterGrid.calcTargetDirection(surroundGrid);
                    result.add(direction);

                    iterGrid = surroundGrid;
                    break;
                }
            }
            if (distance <= 1) {
                break;
            }
        }

        return result;
    }

    public Set<JumpBoardGrid> getOutLineGrids() {
        return outLineGrids;
    }

    public List<JumpBoardGrid> findClockwiseGrids(JumpBoardGrid centerGrid, int distance) {
        List<JumpBoardGrid> result = new ArrayList<>();
        JumpBoardGrid iterGrid = centerGrid;//循环起始点,右上终点
        for (int i = 0; i < distance; i++) {
            List<Integer> pos = iterGrid.findDirectionNeighborPos(eJumpDirection.RightUp);
            int x = pos.get(0);
            int y = pos.get(1);
            JumpBoardGrid rightUpGrid = findGrid(x, y);
            if (rightUpGrid == null) {
                rightUpGrid = new JumpBoardGrid(x, y);
            }
            iterGrid = rightUpGrid;
        }
        List<eJumpDirection> clockwiseDirections = eJumpDirection.RightUp.getClockwiseDirections();
        for (eJumpDirection direction : clockwiseDirections) {
            for (int i = 0; i < distance; i++) {
                List<Integer> pos = iterGrid.findDirectionNeighborPos(direction);
                int x = pos.get(0);
                int y = pos.get(1);
                JumpBoardGrid nextClockwiseGrid = findGrid(x, y);
                if (nextClockwiseGrid != null) {
                    result.add(nextClockwiseGrid);
                } else {
                    nextClockwiseGrid = new JumpBoardGrid(x, y);
                }
                iterGrid = nextClockwiseGrid;
            }
        }
        return result;
    }

    /**
     * 顺时针获取下一个格子
     *
     * @param targetGrid
     * @param centerGrid
     * @return
     */
    public JumpBoardGrid findNextClockwiseGrid(JumpBoardGrid targetGrid, JumpBoardGrid centerGrid) {
        int distance = targetGrid.distanceTo(centerGrid);
        List<JumpBoardGrid> clockwiseGrids = findClockwiseGrids(centerGrid, distance);
        int targetGridIndex = clockwiseGrids.indexOf(targetGrid);
        int nextIndex = targetGridIndex + 1;
        if (nextIndex >= clockwiseGrids.size()) {
            nextIndex = 0;
        }
        JumpBoardGrid nextGrid = clockwiseGrids.get(nextIndex);
        if (!targetGrid.isNeighbor(nextGrid)) {//超出盘面了
            return null;
        }
        return nextGrid;
    }

    private static Integer findDistance2DirectionIndex(int dx, int dy, int dz) {
        // 找到最接近的基本方向
        int closestDirectionIndex = 0;
        int minDistance = Integer.MAX_VALUE;
        List<eJumpDirection> allValidDirections = eJumpDirection.getAllValidDirections();
        for (int i = 0; i < allValidDirections.size(); i++) {
            eJumpDirection direction = allValidDirections.get(i);
            int dxDir = direction.getQrDiff()[0];
            int dyDir = direction.getQrDiff()[1];
            int dzDir = direction.getQrDiff()[2];
            int distance = Math.abs(dx - dxDir) + Math.abs(dy - dyDir) + Math.abs(dz - dzDir);
            if (distance < minDistance) {
                minDistance = distance;
                closestDirectionIndex = i;
            }
        }

        // 根据偏移量调整方向索引
        if (Math.abs(dx) == 2) {
            return closestDirectionIndex;
        } else if (Math.abs(dy) == 2) {
            return (closestDirectionIndex + 2) % 6;
        } else if (Math.abs(dz) == 2) {
            return (closestDirectionIndex + 4) % 6;
        }

        return null;
    }

    // 打印棋盘
    public void printBoard() {
        for (int y = 0; y < size; y++) {
            // 每行前的缩进，模拟偏移以显示六边形结构
            System.out.print(repeat(" ", y % 2 == 0 ? 1 : 0));
            for (int x = 0; x < size; x++) {
                System.out.print((board[y][x] == null ? " " : board[y][x].getPrintf()) + " ");
            }
            System.out.println();
        }
    }

    public void resetPrintf(String str) {
        for (JumpBoardGrid[] jumpBoardGrids : board) {
            for (JumpBoardGrid grid : jumpBoardGrids) {
                if (grid == null) {
                    continue;
                }
                grid.setPrintf(str);
            }
        }
    }

    public static void main(String[] args) {
        HotpotJumpBoard board = new HotpotJumpBoard();

        String black = "⬢";
        String empty = "⬡";

        board.resetPrintf(empty);

        int r = 3;

        JumpBoardGrid findGrid = board.findGrid(9, 4);
        if (findGrid == null) {
            System.out.println("目标点找不到");
            return;
        }
//                findGrid.setPrintf(black);
//        List<JumpGridBase> pathGrids = board.findPathGrids(findGrid, eJumpDirection.LEFT_DOWN);
//        for (JumpGridBase pathGrid : pathGrids) {
//            pathGrid.setPrintf(black);
//        }

//        List<JumpBoardGrid> neighbors = board.findSurroundGrids(findGrid, r);
//        System.out.println("找" + "(X=" + findGrid.getX() + "," + "Y=" + findGrid.getY() + ")周围半径为" + r + "的点");
//        for (JumpBoardGrid neighbor : neighbors) {
//            neighbor.setPrintf(black);
//            System.out.println("(X=" + neighbor.getX() + "," + "Y=" + neighbor.getY() + ")");
//        }

//        //找到指定点的所有直线
//        findGrid.setPrintf(black);
//        for (int y = 0; y < board.board.length; y++) {
//            for (int x = 0; x < board.board[y].length; x++) {
//                JumpBoardGrid jumpBoardGrid = board.board[y][x];
//                if (jumpBoardGrid == null) {
//                    continue;
//                }
//                if (jumpBoardGrid.equals(findGrid)) {
//                    continue;
//                }
//                if (board.arePointsCollinear(jumpBoardGrid, findGrid)) {
//                    eJumpDirection direction = findGrid.calcTargetDirection(jumpBoardGrid);
////                    jumpGridBase.setPrintf(direction.getPrintfStr());
//                    jumpBoardGrid.setPrintf(black);
//                }
//            }
//        }
//        board.printBoard();

//        //打印指定点到其他点的距离
//        for (int y = 0; y < board.board.length; y++) {
//            for (int x = 0; x < board.board[y].length; x++) {
//                JumpGridBase jumpGridBase = board.board[y][x];
//                if (jumpGridBase == null) {
//                    continue;
//                }
//                int distance = findGrid.distanceTo(jumpGridBase);
//                jumpGridBase.setPrintf(String.valueOf(distance));
//            }
//        }

//        //把几个点连线
//        board.resetPrintf(empty);
//        List<JumpBoardGrid> grids = new ArrayList<>();
//        grids.add(board.findGrid(1, 6));
//        grids.add(board.findGrid(3, 6));
//        grids.add(board.findGrid(4, 4));
//        grids.add(board.findGrid(0, 4));
//        grids.add(board.findGrid(2, 0));
//        grids.add(board.findGrid(6, 0));
//        grids.add(board.findGrid(5, 2));
//        grids.add(board.findGrid(2, 8));
//        grids.add(board.findGrid(6, 8));
//        grids.add(board.findGrid(5, 10));
//        grids.add(board.findGrid(7, 10));
//
//        for (JumpBoardGrid grid : grids) {
//            grid.setPrintf(black);
//        }
//        board.printBoard();//把点打印出来
//        System.out.println("---------------------------");
//
//
//        //按顺序把点连线,并给每个点赋予链接的方向
//        Set<JumpBoardGrid> boundaryGrids = new HashSet<>();
//        Map<JumpBoardGrid, Set<eJumpDirection>> gridDirectionMap = new HashMap<>();//每个格子连接的方向
//        for (int i = 0; i < grids.size() - 1; i++) {
//            JumpBoardGrid grid = grids.get(i);
//            JumpBoardGrid nextGrid = grids.get(i + 1);
//            eJumpDirection direction = grid.calcTargetDirection(nextGrid);
//            List<JumpBoardGrid> pathGrids = board.findPathGridsBetweenTowGrids(grid, nextGrid);
//            for (JumpBoardGrid pathGrid : pathGrids) {
//                if (pathGrid.getPrintf().equals(empty)) {
//                    pathGrid.setPrintf(direction.getPrintfStr());
//                }
//                boundaryGrids.add(pathGrid);
//
//                Set<eJumpDirection> directionSet = gridDirectionMap.get(pathGrid);
//                if (directionSet == null) {
//                    directionSet = new HashSet<>();
//                    gridDirectionMap.put(pathGrid, directionSet);
//                }
//                directionSet.add(direction);
//            }
//        }
//        board.printBoard();
//        System.out.println("---------------------------");
//        board.resetPrintf(empty);
//        //找到被点包围的点,不包括路径
//        Set<JumpBoardGrid> endCloseGrids = board.findEndCloseGrids(boundaryGrids, false);
//        for (JumpBoardGrid endCloseGrid : endCloseGrids) {
//            endCloseGrid.setPrintf(black);
//        }
//        board.printBoard();
//        System.out.println("---------------------------");
//        //找到被点包围的点,包括路径
//        endCloseGrids = board.findEndCloseGrids(boundaryGrids, true);
//        for (JumpBoardGrid endCloseGrid : endCloseGrids) {
//            endCloseGrid.setPrintf(black);
//        }
//        board.printBoard();
//        System.out.println("---------------------------");
//        //找到有交叉的点
//        board.resetPrintf(empty);
//        Set<JumpBoardGrid> crossGrids = board.findCrossGridsAndPath(gridDirectionMap, 2);
//        for (JumpBoardGrid crossGrid : crossGrids) {
//            crossGrid.setPrintf(black);
//        }
//        board.printBoard();
//        System.out.println("---------------------------");

//        //找指定点对应指定中心点顺时针的下一个点
//        JumpBoardGrid centerGrid = board.findGrid(5, 4);
//        JumpBoardGrid targetGrid = board.findGrid(7, 5);
//        centerGrid.setPrintf(black);
//        targetGrid.setPrintf(black);
//        board.printBoard();
//        System.out.println("---------------------------");
//        List<JumpBoardGrid> neighbors = board.getNeighbors(centerGrid);
//        for (JumpBoardGrid neighbor : neighbors) {
//            eJumpDirection direction = centerGrid.calcTargetDirection(neighbor);
//            int dq = neighbor.getQ() - centerGrid.getQ();
//            int dr = neighbor.getR() - centerGrid.getR();
//            System.out.println(direction.getPrintfStr()+"dq="+dq+",dr="+dr);
//        }
//        List<JumpBoardGrid> clockwiseGrids = board.findClockwiseGrids(centerGrid, centerGrid.distanceTo(targetGrid));
//        for (JumpBoardGrid clockwiseGrid : clockwiseGrids) {
//            clockwiseGrid.setPrintf(black);
//        }
//        JumpBoardGrid nextClockwiseGrid = board.findNextClockwiseGrid(targetGrid, centerGrid);
//        nextClockwiseGrid.setPrintf(black);
//        for (int i = 0; i < 10; i++) {
//            JumpBoardGrid nextClockwiseGrid = board.findNextClockwiseGrid(targetGrid, centerGrid);
//            nextClockwiseGrid.setPrintf(black);
//            targetGrid = nextClockwiseGrid;
//        }

        HotpotJumpUnit bossUnit = new HotpotJumpUnit();
        JumpBoardGrid bornGrid = board.findGrid(2, 2);
        List<HotpotJumpGrid> bodyGrid = new ArrayList<>();
        for (eJumpDirection direction : eJumpDirection.getAllValidDirections()) {
            List<JumpBoardGrid> pathGrids = board.findPathGrids(bornGrid, direction, 2, false);
            JumpBoardGrid grid = pathGrids.get(pathGrids.size() - 1);
            HotpotJumpGrid jumpGrid = new HotpotJumpGrid(grid.getX(), grid.getY());
            //身体加护盾
            jumpGrid.setShield(1);
            bodyGrid.add(jumpGrid);

            grid.setPrintf(black);
        }
        bossUnit.setHeadPos(new HotpotJumpGrid(bornGrid.getX(), bornGrid.getY()));
        bossUnit.setBodyPos(bodyGrid);

        bornGrid.setPrintf(black);
        board.printBoard();
        System.out.println("---------------------------");
        board.resetPrintf(empty);

        int distanceNeed = 2;
        boolean canMove = true;
        Map<HotpotJumpGrid, JumpBoardGrid> bodyNextBoardGridMap = new HashMap<>();
        JumpBoardGrid iterateGrid = bornGrid;//循环起始点,右上终点
        for (int i = 0; i < distanceNeed; i++) {
            List<Integer> pos = iterateGrid.findDirectionNeighborPos(eJumpDirection.RightUp);
            int x = pos.get(0);
            int y = pos.get(1);
            JumpBoardGrid rightUpGrid = board.findGrid(x, y);
            if (rightUpGrid == null) {
                rightUpGrid = new JumpBoardGrid(x, y);
            }
            iterateGrid = rightUpGrid;
        }
        List<eJumpDirection> clockwiseDirections = eJumpDirection.RightUp.getClockwiseDirections();
        for (eJumpDirection direction : clockwiseDirections) {
            for (int i = 0; i < distanceNeed; i++) {
                List<Integer> pos = iterateGrid.findDirectionNeighborPos(direction);
                int x = pos.get(0);
                int y = pos.get(1);
                JumpBoardGrid nextclockwiseGrid = board.findGrid(x, y);
                if (nextclockwiseGrid == null) {
                    canMove = false;
                    break;
                }
                HotpotJumpGrid bodyJumpGrid = bossUnit.findGridByAll(iterateGrid.getX(), iterateGrid.getY());
                if (bodyJumpGrid != null) {
                    bodyNextBoardGridMap.put(bodyJumpGrid, nextclockwiseGrid);
                }
                iterateGrid = nextclockwiseGrid;
            }
            if (!canMove) {
                break;
            }
        }

        board.printBoard();

    }
}
